﻿CREATE PROCEDURE [dbo].[yaf_message_save](
                @TopicID   INT,
                @UserID    INT,
                @Message   NTEXT,
                @UserName  NVARCHAR(50)  = NULL,
                @IP        NVARCHAR(15),
                @Posted    DATETIME  = NULL,
                @ReplyTo   INT,
                @Flags     INT,
                @MessageID INT  OUTPUT)
AS
    BEGIN
        DECLARE  @ForumFlags INT,
                 @ForumID    INT,
                 @Indent     INT,
                 @Position   INT
        IF @Posted IS NULL
        SET @Posted = Getdate()
        SELECT @ForumID = x.ForumID,
               @ForumFlags = y.Flags
        FROM   yaf_Topic x,
               yaf_Forum y
        WHERE  x.TopicID = @TopicID
        AND y.ForumID = x.ForumID
        IF @ReplyTo IS NULL
        SELECT @Position = 0,
               @Indent = 0 -- New thread
                         
        ELSE
        IF @ReplyTo < 0
        -- Find post to reply to AND indent of this post
        SELECT TOP 1 @ReplyTo = MessageID,
                     @Indent = Indent + 1
        FROM     yaf_Message
        WHERE    TopicID = @TopicID
        AND ReplyTo IS NULL
        ORDER BY Posted
        ELSE
        -- Got reply, find indent of this post
        SELECT @Indent = Indent + 1
        FROM   yaf_Message
        WHERE  MessageID = @ReplyTo
        -- Find position
        IF @ReplyTo IS NOT NULL
        BEGIN
            DECLARE  @temp INT
                           
            SELECT @temp = ReplyTo,
                   @Position = Position
            FROM   yaf_Message
            WHERE  MessageID = @ReplyTo
                        
            IF @temp IS NULL
            -- We are replying to first post
            SELECT @Position = MAX(Position) + 1
            FROM   yaf_Message
            WHERE  TopicID = @TopicID
                      
            ELSE
            -- Last position of replies to parent post
            SELECT @Position = MIN(Position)
            FROM   yaf_Message
            WHERE  ReplyTo = @temp
            AND Position > @Position
                           
            -- No replies, THEN USE parent post's position+1
            IF @Position IS NULL
            SELECT @Position = Position + 1
            FROM   yaf_Message
            WHERE  MessageID = @ReplyTo
            -- Increase position of posts after this
            UPDATE yaf_Message
            SET    Position = Position + 1
            WHERE  TopicID = @TopicID
            AND Position >= @Position
        END
        -- Add points to Users total points
        UPDATE yaf_User
        SET    Points = Points + 3
        WHERE  UserID = @UserID
        INSERT yaf_Message
              (UserID,
               Message,
               TopicID,
               Posted,
               UserName,
               IP,
               ReplyTo,
               Position,
               Indent,
               Flags)
        VALUES(@UserID,
               @Message,
               @TopicID,
               @Posted,
               @UserName,
               @IP,
               @ReplyTo,
               @Position,
               @Indent,
               @Flags & ~ 16)
        SET @MessageID = Scope_identity()
        IF (@ForumFlags & 8) = 0
        EXEC yaf_message_approve
             @MessageID
    END


